package com.scorpio.baselibrary.utils;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * created by zhud on 2019/9/30
 */
public class ConvertUtils {
    /**
     * 把modelA对象的属性值赋值给bClass对象的属性。
     *
     * @param modelA
     * @param bClass
     * @param <T>
     * @return
     */
    public static <A, T> T sourceToTarget(A modelA, Class<T> bClass) {
        try {
            Gson gson = new Gson();
            String gsonA = gson.toJson(modelA);
            T instanceB = gson.fromJson(gsonA, bClass);
            return instanceB;
        } catch (Exception e) {
            return null;
        }
    }

    public static <A, T> List<T> sourceToTarget(List<A> modelA, Class<T> bClass) {
        List<T> lists = new ArrayList<>(modelA.size());
        for (Object source : modelA) {
            lists.add(sourceToTarget(source, bClass));
        }
        return lists;
    }
}
